cmake_minimum_required(VERSION 3.10)

project(network_framework)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

file(GLOB_RECURSE src_files CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/src/*.cpp"
    "${CMAKE_SOURCE_DIR}/src/*.cc")

# 执行 build_protobuf.sh 脚本
add_custom_command(
    OUTPUT ${CMAKE_BINARY_DIR}/protobuf_built.txt
    COMMAND ${CMAKE_COMMAND} -E env bash ${CMAKE_SOURCE_DIR}/src/net/network_message/protobuf/build_protobuf.sh
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/net/network_message/protobuf
    COMMENT "Running build_protobuf.sh script"
    VERBATIM
)

# 添加自定义目标来触发自定义命令
add_custom_target(run_protobuf ALL
    DEPENDS ${CMAKE_BINARY_DIR}/protobuf_built.txt
)

set(include_dir
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/db
    ${CMAKE_SOURCE_DIR}/src/game
    ${CMAKE_SOURCE_DIR}/src/game/game_record
    ${CMAKE_SOURCE_DIR}/src/user
    ${CMAKE_SOURCE_DIR}/src/net
    ${CMAKE_SOURCE_DIR}/third_party/mysql_cpp_connx/include)


add_library(${PROJECT_NAME} ${src_files})


target_link_libraries(${PROJECT_NAME} PRIVATE pthread mysqlcppconnx)   # crypto ssl

if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|i686|i386")
    message(STATUS "Current architecture: x86/x86_64")
    set(link_dirs ${CMAKE_SOURCE_DIR}/third_party/mysql_cpp_connx/lib/x86-64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
    if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
        message(STATUS "Current OS: MacOS")
        set(link_dirs ${CMAKE_SOURCE_DIR}/third_party/mysql_cpp_connx/lib/aarch64/MacOS)
    elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
        message(STATUS "Current OS: Linux")
        set(link_dirs ${CMAKE_SOURCE_DIR}/third_party/mysql_cpp_connx/lib/aarch64/Linux)
    else()
        message(WARNING "Unsupported OS: ${CMAKE_SYSTEM_NAME}")
    endif()
else()
    message(WARNING "Unknown architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
target_link_directories(${PROJECT_NAME} PRIVATE ${link_dirs})

target_include_directories(${PROJECT_NAME} PRIVATE ${include_dir})

find_package(OpenSSL REQUIRED)
if (OpenSSL_FOUND)
    message("${PROJECT_NAME}: OpenSSL found, libraries: ${OpenSSL_LIBRARIES}")
    target_link_libraries(${PROJECT_NAME} ${OpenSSL_LIBRARIES})
else()
    message(FATAL_ERROR "OpenSSL not found")
endif()

# find_package(protobuf CONFIG REQUIRED)
find_package(Boost REQUIRED)

if (Boost_FOUND)
    message("${PROJECT_NAME}: Boost found, libraries: ${Boost_LIBRARIES}")
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
    target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "Boost not found")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    find_package(protobuf CONFIG REQUIRED)
    target_link_libraries(${PROJECT_NAME} PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_libraries(${PROJECT_NAME} PRIVATE protobuf)
endif()
# 
set(server_out ${PROJECT_NAME}_server.out)
add_executable(${server_out} src/server_main.cpp)
target_link_libraries(${server_out} ${PROJECT_NAME})
target_include_directories(${server_out} PUBLIC ${include_dir})
target_link_directories(${server_out} PRIVATE ${link_dirs})

if (Boost_FOUND)
    target_include_directories(${server_out} PRIVATE ${Boost_INCLUDE_DIRS})
endif()

# 
set(client_out ${PROJECT_NAME}_client.out)
add_executable(${client_out} src/client_main.cpp)
target_link_libraries(${client_out} ${PROJECT_NAME})
target_include_directories(${client_out} PUBLIC ${include_dir})
target_link_directories(${client_out} PRIVATE ${link_dirs})

if (Boost_FOUND)
    target_include_directories(${client_out} PRIVATE ${Boost_INCLUDE_DIRS})
endif()
